﻿package {
	import flash.display.MovieClip;
	import flash.net.ObjectEncoding;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.events.*;
	import flash.display.*;
	import flash.display.LoaderInfo;
	import flash.display.Loader;
	import flash.system.*;
	import flash.text.TextField;

	public class player extends MovieClip {
		var host:String = "64.22.126.39";
		//var host:String = "localhost";
		var filename:String;
		var nc:NetConnection;
		var ns1:NetStream;
		var connected:int;
		var playerVersion:String;
		
		public function player() {
			
			playing1.visible = false;
			stopped1.visible = false;
			error1.visible = false;
			
			//check for flash player version
			var ver = getVersion();
			if (ver == "9"){
				txt_ver.visible = false;
			}
			//get file name parameter
			filename = root.loaderInfo.parameters.filename;
			//if (filename == null) debug.text = "null" else debug.text = filename;
			//stop movie from running
			stop();
			
			
			//create new net connection
			nc=new NetConnection()  ;
			nc.proxyType = "best";
			// set encoding to old amf
			nc.objectEncoding=ObjectEncoding.AMF0;
			// netstatus event listening
			nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
			// connect to red5, passing false as parameter
			trace("rtmp://"+host+"/firstapp","test1");
			nc.connect("rtmp://"+host+"/firstapp","test1");
			//bla bla bla
			//bla bla bla
			//bla bla bla
			//bla bla bla
			
		}



		function getAudio():void {
			//create net stream
			ns1 = new NetStream(nc);
			//ns1.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			ns1.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			//attach client
			ns1.client = new CustomClient(this);
			
			connecting1.visible = false;
			playing1.visible = true;
			//play sound
			ns1.receiveAudio(true);
			ns1.play(filename);
		}

		function netStatus(event:NetStatusEvent):void {
			//trace message
			trace(event.info.code);
			if (event.info.code == "NetConnection.Connect.Rejected") {
				connecting1.visible = false;
				error1.visible = true;
			} else if (event.info.code == "NetConnection.Connect.Failed") {
				connecting1.visible = false;
				error1.visible = true;
			} else if (event.info.code == "NetConnection.Connect.Success"){
				if (connected == 0) {
					connected == 1;
					getAudio();
				}
			}
		}
		
		function asyncErrorHandler(event:AsyncErrorEvent):void {
			playing1.visible = false;
			error1.visible = true;

		}
		
		//Change status from playing to stopped
		function myStatus(stat:int):void {
			if (stat == 1) {
				playing1.visible = true;
				stopped1.visible = false;

			} else {
				playing1.visible = false;
				stopped1.visible = true;
				ns1.close();
				nc.close();
			}
		}
		
		//get flash player version
		function getVersion():String {
			var versionString:String = Capabilities.version;
			var pattern:RegExp = /^(\w*) (\d*),(\d*),(\d*),(\d*)$/;
			var result:Object = pattern.exec(versionString);
			if (result != null) {
				trace("input: " + result.input);
				trace("platform: " + result[1]);
				trace("majorVersion: " + result[2]);
				trace("minorVersion: " + result[3]);
				trace("buildNumber: " + result[4]);
				trace("internalBuildNumber: " + result[5]);
			} else {
				trace("Unable to match RegExp.");
			}
			return result[2];
		}



	}
}